﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WindowsLive.Writer.HtmlParser.Parser;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Diagnostics;
using System.Windows.Media.Imaging;

namespace WLG.WPFView.Helpers
{
    public class HTMLToXamlHelper
    {
        public List<UIElement> GetXamlFromHTML(string data)
        {
            List<UIElement> result = new List<UIElement>();

            SimpleHtmlParser parser = new SimpleHtmlParser(data);
            Element currentElement = parser.Next();



            while (currentElement != null)
            {
                if (currentElement is BeginTag)
                {
                    string styleData = ".DummyClassName{" + ((BeginTag)currentElement).GetAttributeValue("style") + "}";
                    if (styleData == null)
                        styleData = "";
                    CssParser cssParser = new CssParser(styleData);
                    if (((BeginTag)currentElement).Name.ToLower() == "a")
                    {
                        TextBlock tb = new TextBlock();
                        tb.Style = (Style)Application.Current.FindResource("LiveHyperlink");

                        if (currentElement.RawText.EndsWith("/>"))
                        {

                            Hyperlink hl = new Hyperlink();
                            tb.Inlines.Add(hl);

                            hl.RequestNavigate += new RequestNavigateEventHandler(hl_RequestNavigate);
                            hl.NavigateUri = new Uri(((BeginTag)currentElement).GetAttribute("href").Value);
                            hl.Inlines.Add("test");

                            result.Add(tb);
                        }
                        else
                        {
                            Hyperlink hl = new Hyperlink();
                            tb.Inlines.Add(hl);

                            //hl.TargetName = "_blank";
                            hl.NavigateUri = new Uri(((BeginTag)currentElement).GetAttribute("href").Value);

                            // Get the content tag
                            Element swapElement = currentElement;
                            currentElement = parser.Next();
                            if (currentElement is Text)
                                hl.Inlines.Add(currentElement.RawText);
                            else
                                currentElement = swapElement;

                            result.Add(tb);
                        }
                    }
                    else if (((BeginTag)currentElement).Name.ToLower() == "img")
                    {
                        Image i = new Image();
                        i.MaxHeight = 50;
                        i.MaxWidth = 50;
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.UriSource = new Uri(((BeginTag)currentElement).GetAttribute("src").Value);
                        bi.EndInit();
                        i.Source = bi;
                        result.Add(i);
                    }
                }
                else if (!(currentElement is EndTag))
                {
                    TextBlock tb = new TextBlock();
                    tb.Text = currentElement.RawText;
                    result.Add(tb);
                }

                currentElement = parser.Next();
            }

            return result;
        }

        void hl_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {

            string navigateUri = ((Hyperlink)sender).NavigateUri.ToString();
            Process.Start(new ProcessStartInfo(navigateUri));
            e.Handled = true;

        }


    }
}
